#include "IServlet.h"
#include "TestThreadServlet.h"
#include "ISignal.h"
#include "ILogger.h"
void TestThreadServlet::onStart()
{
    LogI("start to test thread");
    IServlet *server = Bean<IServlet>("threadServlet").pointer(); 
    if (server) {
        server->on(STREAM,[](ISession *session,void *data,int size)->int{
            LogI("recvInServlet:%s",(char*)data);
            session->reply("2222",5);
            return 0;
        });
    }

    IServlet *client = Bean<IServlet>("threadClient").pointer();
    if (client) {
        client->on(STREAM,[&](ISession *session,void *data,int size)->int{
            LogI("recvInClient:%s",(char*)data);
            return 0;
        });
        client->request("3333",5); 
    }
 
    ISignal *singal = Bean<ISignal>("signal").pointer();
    if (singal) {
        singal->subscrible(CTRL_C,this);
        singal->subscrible(CTRL_Z,this);
    }


    
    IServlet *fdbus_server = Bean<IServlet>("fdbus_server").pointer();
    if (fdbus_server) {
        fdbus_server->on(STREAM,[&](ISession *session,void *data,int size)->int{
            LogI("FDBUS recvInServer:%s",(char*)data);
            session->reply("5555",5);
            return 0;
        });
    }
    IServlet *fdbus_client = Bean<IServlet>("fdbus_client").pointer();
    if (fdbus_client) {
        fdbus_client->on(STREAM,[=](ISession *session,void *data,int size)->int{
             
            LogI("FDBUS recvInClient:%s",(char*)data);
           
            return 0;
        });
        fdbus_client->request("4444",5);
    }

    plugins.push_back(server);
    plugins.push_back(client);
}
void TestThreadServlet::handleSignal(int sig)
{
    LogI("handleSignal callback :%d",sig);
    for (auto ii = plugins.begin(); ii != plugins.end();++ii)
    {
        ((IServlet*)(*ii))->stop();
    }
}


REG_PLUGIN(TestThreadServlet);